package cn.xy.user.service.impl;

import cn.xy.basic.constants.BaseConstant;
import cn.xy.basic.dto.RegisterDto;
import cn.xy.basic.exception.BusinessException;
import cn.xy.basic.util.MD5Utils;
import cn.xy.basic.util.StrUtils;
import cn.xy.user.domain.Logininfo;
import cn.xy.user.domain.User;
import cn.xy.user.mapper.LogininfoMapper;
import cn.xy.user.mapper.UserMapper;
import cn.xy.user.service.IUserService;
import cn.xy.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xy
 * @since 2022-06-16
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 手机号注册
     */
    public void phoneRegister(RegisterDto dto){
        //先获取手机号，手机验证码，密码和确认密码,做一个校验，
        String phone = dto.getPhone();
        String smsCode = dto.getSmsCode();
        String password = dto.getPassword();
        String passwordRepeat = dto.getPasswordRepeat();

        //先空校验
        if(phone == null){
            throw new BusinessException("请输入手机号");
        }
        if(smsCode == null){
            throw new BusinessException("请输入验证码");
        }
        if(password == null){
            throw new BusinessException("请输入密码");
        }
        if(passwordRepeat == null){
            throw new BusinessException("请输入确认密码");
        }
        //都不为空，再进行判断
        //判断密码和确认密码是否一致
        if(!password.equals(passwordRepeat)){
            throw new BusinessException("两次密码不一致");
        }

        //到此就表示信息都不为空并且密码两次都一致。
        //短信验证码判断
        //创建验证码存储在redis的key的固定格式
        String key = BaseConstant.RegisterConstants.REGISTER_SMS_PREFIX + phone;
        //通过key去redis缓存中拿取值
        Object smsCodeTmp = redisTemplate.opsForValue().get(key);
        //判断，
        if(smsCodeTmp == null){
            //为空，则说明验证码已经过期被销毁。
            throw new BusinessException("验证码已失效，请重新获取");
        }

        //拿到验证码，再判断是否与输入的一致
        if(!smsCode.equalsIgnoreCase(smsCodeTmp.toString().split(":")[0])){
            //不一致
            throw new BusinessException("短信验证码错误，请重试");
        }

        //一致
        //保存数据
        User user = dto2User(dto);
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }


    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        //手动设置tupe，
        logininfo.setType(1);
        return logininfo;
    }

    private User dto2User(RegisterDto dto) {
        User user = new User();
        user.setUsername(dto.getPhone());
        user.setPhone(dto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        return user;
    }

    /**
     * 级联操作
     * @param user
     */
    @Override
    public void update(User user) {
        super.update(user);
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
    }
}
